package cn.iocoder.idata.module.idata.controller.admin.object;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.object.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.object.ObjectDO;
import cn.iocoder.idata.module.idata.convert.object.ObjectConvert;
import cn.iocoder.idata.module.idata.service.object.ObjectService;

@Tag(name = "管理后台 - 标注物管理")
@RestController
@RequestMapping("/idata/object")
@Validated
public class ObjectController {

    @Resource
    private ObjectService objectService;

    @PostMapping("/create")
    @Operation(summary = "创建标注物管理")
    @PreAuthorize("@ss.hasPermission('idata:object:create')")
    public CommonResult<Long> createObject(@Valid @RequestBody ObjectCreateReqVO createReqVO) {
        return success(objectService.createObject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标注物管理")
    @PreAuthorize("@ss.hasPermission('idata:object:update')")
    public CommonResult<Boolean> updateObject(@Valid @RequestBody ObjectUpdateReqVO updateReqVO) {
        objectService.updateObject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标注物管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('idata:object:delete')")
    public CommonResult<Boolean> deleteObject(@RequestParam("id") Long id) {
        objectService.deleteObject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标注物管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('idata:object:query')")
    public CommonResult<ObjectRespVO> getObject(@RequestParam("id") Long id) {
        ObjectDO object = objectService.getObject(id);
        return success(ObjectConvert.INSTANCE.convert(object));
    }

    @GetMapping("/list")
    @Operation(summary = "获得标注物管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('idata:object:query')")
    public CommonResult<List<ObjectRespVO>> getObjectList(@RequestParam("ids") Collection<Long> ids) {
        List<ObjectDO> list = objectService.getObjectList(ids);
        return success(ObjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标注物管理分页")
    @PreAuthorize("@ss.hasPermission('idata:object:query')")
    public CommonResult<PageResult<ObjectRespVO>> getObjectPage(@Valid ObjectPageReqVO pageVO) {
        PageResult<ObjectDO> pageResult = objectService.getObjectPage(pageVO);
        return success(ObjectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标注物管理 Excel")
    @PreAuthorize("@ss.hasPermission('idata:object:export')")
    @OperateLog(type = EXPORT)
    public void exportObjectExcel(@Valid ObjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ObjectDO> list = objectService.getObjectList(exportReqVO);
        // 导出 Excel
        List<ObjectExcelVO> datas = ObjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "标注物管理.xls", "数据", ObjectExcelVO.class, datas);
    }

}
